<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>良田webCamera</title>
    <script type="text/javascript" src="js/jquery.min.js"></script>
    <script type="text/javascript" src="js/eloamWebCamera.js"></script>
</head>
<style>
    .eloam-but {
        width: 160px;
        margin-left: 20px;
        margin-top: 10px;
        padding: 2px;
    }
</style>
<body>
    <div>
        <table height="60">
            <tr>
                <td>
                    <div>
                        <div style="padding-top: 20px;margin-left: 20px;width: 800px">
                            <div id="noniediv">
                                <img id="ImgMain" width="760" height="500"/>
                            </div>
                        </div>
                        <input type="button" title="打开摄像头" onclick="openPhoto()" value="打开摄像头" class="eloam-but"/>
                        <input type="button" title="拍照" onclick="getBase64()" value="拍照" class="eloam-but"/>
                        <input type="button" title="关闭摄像头" onclick="closePhoto()" value="关闭" class="eloam-but"/>
                        <div id="outerdiv"
                             style="position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:999;width:100%;height:100%;display:none;">
                            <div id="innerdiv" style="position:absolute;">
                                <img id="bigimg" style="border:5px solid #fff;" src=""/>
                            </div>
                        </div>
                    </div>
                </td>
                <td>
                    <div>
                    <div id="imgList"></div>
                    </div>
                </td>
            </tr>
        </table>
    </div>
</body>
<script>
    var webCamera;

    function init() {
        console.log("初始化");
         webCamera = new WebCamera($, {
             initCamera: true, // 是否要初始化摄像头状态，如果为true,则会检查摄像头状态，如果是打开的，会将摄像头关闭
             hideDivImg: false,
             port: '38088',// CameraWebService 服务端口，默认38088
             imgMainDivId: 'noniediv',// 包含img div 的id,
             imgMainId: 'ImgMain',// 非IE img id 显示预览
        }, function (data) {//成功回调
            console.log("初始化成功");
             openDev('0');
        }, function (err) {//失败回调
            console.log(err)
        })
    }

    function addImgDiv(imgPath) {
        if (!imgPath) return;
        if (imgPath.indexOf('base64,') < 0) {
            imgPath = 'data:image/jpg;base64,' + imgPath
        }
        var item = '<img height="100" width="100" src="' + imgPath + '" class="pic" onclick=\'openImage(this)\' />'
        $('#imgList').append(item);
        var id = getQueryString('id');
        console.log(id);
        $.ajax({
            url: "/open/post/file",
            type: "post",
            data: {
                "base64": imgPath,
                "id" : id
            },
            async: false,//异步请求
            success: function (data) {
                alert("success");
            },
            error: function (data) {
                alert("error");
            }
        });
    }

    function openImage(imageItem) {
        imgShow("#outerdiv", "#innerdiv", "#bigimg", imageItem);
    }

    function imgShow(outerdiv, innerdiv, bigimg, _this) {
        var src = _this.src;//获取当前点击的pimg元素中的src属性
        $(bigimg).attr("src", src);//设置#bigimg元素的src属性
        /*获取当前点击图片的真实大小，并显示弹出层及大图*/
        $("<img/>").attr("src", src).load(function () {
            var windowW = $(window).width();//获取当前窗口宽度
            var windowH = $(window).height();//获取当前窗口高度
            var realWidth = this.width;//获取图片真实宽度
            var realHeight = this.height;//获取图片真实高度
            var imgWidth, imgHeight;
            var scale = 0.8;//缩放尺寸，当图片真实宽度和高度大于窗口宽度和高度时进行缩放
            if (realHeight > windowH * scale) {//判断图片高度
                imgHeight = windowH * scale;//如大于窗口高度，图片高度进行缩放
                imgWidth = imgHeight / realHeight * realWidth;//等比例缩放宽度
                if (imgWidth > windowW * scale) {//如宽度扔大于窗口宽度
                    imgWidth = windowW * scale;//再对宽度进行缩放
                }
            } else if (realWidth > windowW * scale) {//如图片高度合适，判断图片宽度
                imgWidth = windowW * scale;//如大于窗口宽度，图片宽度进行缩放
                imgHeight = imgWidth / realWidth * realHeight;//等比例缩放高度
            } else {//如果图片真实高度和宽度都符合要求，高宽不变
                imgWidth = realWidth;
                imgHeight = realHeight;
            }
            $(bigimg).css("width", imgWidth);//以最终的宽度对图片缩放
            var w = (windowW - imgWidth) / 2;//计算图片与窗口左边距
            var h = (windowH - imgHeight) / 2;//计算图片与窗口上边距
            $(innerdiv).css({"top": h, "left": w});//设置#innerdiv的top和left属性
            $(outerdiv).fadeIn("fast");//淡入显示#outerdiv及.pimg
        });
        $(outerdiv).click(function () {//再次点击淡出消失弹出层
            $(this).fadeOut("fast");
        });
    }

    function openDev(index) {
        if (!index) {
            index = 0
        }
        devIndex = index;
        var data = {
            devIndex: index // 摄像头下标 0主头；1副头
        };
        webCamera.openCamera(data, function (data) {
            console.log("开启主摄像头");
        }, function (err) {
            console.log(err)
        })
    }

    function closePhoto() {
        var data = {
            devIndex: 0 // 摄像头下标
        };
        webCamera.closeCamera(data, function (data) {
            console.log(data)
        }, function (err) {

        })
    }

    function getQueryString(name) {     //name为传入参数
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }

    function openPhoto() {
        init();
    }

    function getBase64() {
        var data = {
            devIndex: devIndex,// 拍照摄像头
            rotate: '',//旋转角度
            cutpage: 0
        };
        webCamera.grabImageBase64(data, function (data) {
            addImgDiv(data.photoBase64)
        }, function (err) {
            console.log(err)
        })
    }
</script>
</body>
</html>
